package com.binguo.service;

import com.alibaba.fastjson.JSONObject;
import com.binguo.common.util.PriceUtil;
import com.binguo.model.mapper.WxRechargeDiscountMapper;
import com.binguo.model.model.Customer;
import com.binguo.model.model.CustomerExtend;
import com.binguo.model.model.WxRechargeDiscount;
import com.binguo.model.repository.WxRechargeDiscountRepository;
import com.google.gson.internal.$Gson$Types;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Condition;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author yuritian
 * @Title: RechargeService
 * @ProjectName binguoDevelop
 * @Description: TODO
 * @date 2020/5/1213:56
 */
@Service
public class RechargeService {

    private static Logger logger = LoggerFactory.getLogger(RechargeService.class);


    @Resource
    WxRechargeDiscountRepository wxRechargeDiscountRepository;

    @Resource
    CustomerService customerService;

    public JSONObject getWxRechargeMoney(Integer shopId,
                                         Integer customerId,
                                         Long rechargeMoney){
        JSONObject result = new JSONObject();

        CustomerExtend customerExtend = customerService.getCustomerExtend(customerId,shopId);

        Condition condition = new Condition(WxRechargeDiscount.class);
        condition.or().andEqualTo("status",1)
                .andEqualTo("customerGroupId",customerExtend.getGroupId())
                .andEqualTo("shopId",shopId);
        condition.orderBy("rechargeMostMoney");

        Long value = null;
        Integer type = null;
        String note = "";

        List<WxRechargeDiscount> discountList = wxRechargeDiscountRepository.findByCondition(condition);

        if (discountList != null && discountList.size() > 0){
            for (WxRechargeDiscount discount : discountList){
                if (rechargeMoney >= discount.getRechargeLeastMoney()){
                    if ((discount.getRechargeMostMoney() != null && rechargeMoney < discount.getRechargeMostMoney())
                        || discount.getRechargeMostMoney() == null){
                        value = discount.getValue();
                        type = discount.getType();
                        if (discount.getRechargeMostMoney() != null) {
                            note = "本次充值优惠享受的价格区间为" + PriceUtil.changeF2Y(discount.getRechargeLeastMoney())
                                    + "元---" + PriceUtil.changeF2Y(discount.getRechargeMostMoney())
                                    + "元,";
                        }else {
                            note = "本次充值优惠享受的价格区间为" + PriceUtil.changeF2Y(discount.getRechargeLeastMoney())
                                    + "元以上";
                        }
                        break;
                    }
                }
            }
            if (type != null) {
                if (type.equals(1)) {
                    note += "充值优惠方式为:赠送"+(value-100)+"%";
                    rechargeMoney = rechargeMoney * value / 100;
                } else if (type.equals(2)) {
                    note += "充值优惠方式为:赠送"+PriceUtil.changeF2Y(value)+"元";
                    rechargeMoney = rechargeMoney + value;
                }
            }
        }

        logger.info("充值金额为"+rechargeMoney);

        result.put("rechargeMoney",rechargeMoney);
        result.put("note",note);

        return result;
    }
}
